package com.android.jixin.channel;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.jixin.channel.base.BaseActivity;
import com.android.jixin.channel.presenter.impl.MainPresenterImpl;
import com.android.jixin.channel.ui.fragment.HomeFragment;
import com.android.jixin.channel.view.IMainView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity<MainPresenterImpl> implements IMainView {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.home)
    TextView home;
    @BindView(R.id.message)
    TextView message;
    @BindView(R.id.my)
    TextView my;

    private long exitTime = 0;

    @Override
    protected void initView() {
        getSupportActionBar().setHomeButtonEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        mPresenter.getData();

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.container, HomeFragment.newInstance()).commit();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initInjector() {
        getActivityComponent().inject(this);
    }

    @OnClick({R.id.home, R.id.message, R.id.my})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home:
                break;
            case R.id.message:
                break;
            case R.id.my:
                break;
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            showMsg("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }
}
